home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / DBLROUND.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  653b  |  36 lines

  1. /*
  2. **  DBLROUND.C - Rounds a double to the nearest whole number
  3. **  public domain by Ross Cottrell
  4. */
  5.  
  6. #include <float.h>
  7. #include <limits.h>
  8. #include <assert.h>
  9.  
  10. double round(double x)
  11. {
  12.       assert(1 == FLT_ROUNDS);
  13.       x += 1.0 / DBL_EPSILON;
  14.       return x - 1.0 / DBL_EPSILON;
  15. }
  16.  
  17. #ifdef TEST
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. void main(int argc, char *argv[])
  23. {
  24.       double val;
  25.       char *dummy;
  26.  
  27.       while (--argc)
  28.       {
  29.             val = strtod((const char *)(*(++argv)), &dummy);
  30.             printf("round(%g) = ", val);
  31.             printf("%.12g\n", round(val));
  32.       }
  33. }
  34.  
  35. #endif /* TEST */
  36.